package com.ews.onlineexchange.app;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.security.RolesAllowed;
import javax.enterprise.inject.Model;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;

import com.ews.onlineexchange.model.Bulletin;
import com.ews.onlineexchange.model.DataEntity;
import com.ews.onlineexchange.model.ServiceSupplier;
import com.ews.onlineexchange.model.Trade;
import com.ews.onlineexchange.model.TradeStatus;
import com.ews.onlineexchange.service.EntityQuery;

/**
 * 中介机构用户查询本用户中标的所有交易清单
 * @author admin
 *
 */
@Model
@RolesAllowed(value = "supplier")
public class SupplierSuccessTradeListForApp extends EntityQuery<Trade> {

	/**
	 * 
	 */ 
	private static final long serialVersionUID = 1L;
	
	public SupplierSuccessTradeListForApp() {
		super();
		setOrder("createDate DESC");
	}
	private String app_userId;
	private String projectName;
	private String serialNumber;	

	@Override
	protected List<Predicate> getRestrictions(CriteriaBuilder cb, Root<Trade> entity) {
		
		List<Predicate> restrictions = new ArrayList<Predicate>();
		restrictions.add(cb.equal(entity.get("delFlag"), DataEntity.DEL_FLAG_NORMAL));
		restrictions.add(cb.isTrue(entity.<Boolean>get("apply")));
		Join<Trade, ServiceSupplier> supplier = entity.join("supplier");
		restrictions.add(cb.equal(supplier.get("id"), app_userId));
		
		Join<Trade, Bulletin> bulletin = entity.join("bulletin");
		restrictions.add(cb.isTrue(bulletin.<Boolean>get("over")));		
		restrictions.add(cb.equal(bulletin.get("tradeStatus"), TradeStatus.Agreed));
		restrictions.add(cb.equal(bulletin.get("successTrade"), entity));
		//过滤已经取消的公告
		restrictions.add(cb.isFalse(bulletin.<Boolean>get("cancelled")));
		
	
		if (!StringUtils.isBlank(projectName))
			restrictions.add(cb.like(bulletin.get("projectName"), getStringRestriction(projectName)));
		
		if (!StringUtils.isBlank(serialNumber))
			restrictions.add(cb.like(bulletin.get("serialNumber"), getStringRestriction(serialNumber)));
		
		return restrictions;
	}

	public String getApp_userId() {
		return app_userId;
	}

	public void setApp_userId(String app_userId) {
		this.app_userId = app_userId;
	}

	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	public String getSerialNumber() {
		return serialNumber;
	}

	public void setSerialNumber(String serialNumber) {
		this.serialNumber = serialNumber;
	}
	
	
}
